package demo3;
/**
 * 下面的这个是张磊老师的文件读写的代码
 */

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class FileUtil {
	/**
	 * 读取文件
	 * 
	 * @param urlString
	 * @return
	 */
	public static String readFile(String urlString) throws Exception {
		StringBuffer content = new StringBuffer();
		FileReader fr = new FileReader(urlString);
		BufferedReader reader = new BufferedReader(fr);
		String line = "";
		while ((line = reader.readLine()) != null) {
			content.append(line + "\n");
		}
		reader.close();
		String contentString = content.toString();
		return contentString;
	}
	
	/**
	 *
	 * 写入文件
	 * @param filePath
	 * @param txt
	 */
	public static void writeFile(String filePath, String txt) {
		FileWriter fw = null;
		try {
			fw = new FileWriter(filePath, true);
			fw.write(txt + "\r\n");
			fw.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}
	
	public static void main(String[] args) {
		try {
			//writeFile("E:\\我的大学之路\\123.txt","你好");
			BufferedReader reader1 = new BufferedReader(new FileReader("E:\\我的大学之路\\test1.txt"));
			BufferedReader reader2 = new BufferedReader(new FileReader("E:\\我的大学之路\\test2.txt"));
			FileWriter writer = new FileWriter("E:\\我的大学之路\\test3.txt");

			String line1;
			String line2;
			while ((line1 = reader1.readLine())!= null && (line2 = reader2.readLine())!= null) {
				int num1 = Integer.parseInt(line1);
				int num2 = Integer.parseInt(line2);
				int sum = num1 + num2;
				writer.write(String.valueOf(sum) + "\r\n");
			}

			reader1.close();
			reader2.close();
			writer.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
